package com.kzd.btgame.main

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.kzd.btgame.AgreeActivity
import com.kzd.btgame.MainActivity
import com.kzd.btgame.entity.*
import com.kzd.btgame.main.game.*
import com.kzd.btgame.main.home.*
import com.kzd.btgame.main.mine.*
import com.kzd.btgame.main.money.*
import com.kzd.btgame.main.service.ProblemDetailsActivity

const val AUTH_KEY_UID = "uid"
const val AUTH_REQUEST_CODE = 1
const val AUTH_RESULT_CODE = 100
const val AGREE_REQUEST_CODE = 2
const val AGREE_RESULT_CODE = 100

fun toActivity(page: String, isLogin: Int = 0) {
    val uri = Uri.parse(page)
    val path = uri.path
        ?: return
    val bundle = Bundle().apply {
        (uri.encodedQuery?.split(("&")) ?: return@apply).forEach {
            val keyValue = it.split(("=")).toTypedArray()
            if (keyValue.size == 2) {
                putString(keyValue[0], keyValue[1])
            }
        }
    }
    if (isLogin == 0) {
        ActivityUtils.startActivity(bundle, AppUtils.getAppPackageName(), path)
    } else {
        checkLogin(path)
    }
}

/**
 * 跳转-协议页面
 * */
fun toAgreeActivity(activity: Activity? = null) {
    if (activity == null) {
        ActivityUtils.startActivity(AgreeActivity::class.java)
    } else {
        ActivityUtils.startActivityForResult(
            activity,
            AgreeActivity::class.java,
            AGREE_REQUEST_CODE
        )
    }
}

const val KEY_MAIN_INDEX = "KEY_MAIN_INDEX"

/**
 * 跳转-主页面 com.kzd.btgame.MainActivity
 *
 * @param index 0=首页1=游戏2=赚金3客服4=我的
 * */
fun toMainActivity(index: Int = 0) {
    val bundle = Bundle().apply {
        putString(KEY_MAIN_INDEX, index.toString())
    }
    ActivityUtils.startActivity(bundle, MainActivity::class.java)
}

/**
 * 跳转-账号交易 com.kzd.btgame.main.home.HomeBargainActivity
 * */
fun toHomeBargainActivity() {
    ActivityUtils.startActivity(HomeBargainActivity::class.java)
}

/**
 * 跳转-账号交易小号回收 com.kzd.btgame.main.home.BargainBuyActivity
 * */
fun toBargainBuyActivity() {
    checkLogin(BargainBuyActivity::class.java)
}

const val KEY_BARGAIN_DETAILS_ID = "KEY_BARGAIN_DETAILS_ID"

/**
 * 跳转-账号交易商品详情 com.kzd.btgame.main.home.BargainDetailsActivity
 *
 * @param bargainId 交易id
 * */
fun toBargainDetailsActivity(bargainId: String) {
    val bundle = Bundle().apply {
        putString(KEY_BARGAIN_DETAILS_ID, bargainId)
    }
    ActivityUtils.startActivity(bundle, BargainDetailsActivity::class.java)
}

const val KEY_BARGAIN_DETAILS_ENTITY = "KEY_BARGAIN_DETAILS_ENTITY"

/**
 * 跳转-账号交易支付 com.kzd.btgame.main.home.BargainPaymentActivity
 * */
fun toBargainPaymentActivity(bargainDetails: BargainDetails) {
    val bundle = Bundle().apply {
        putSerializable(KEY_BARGAIN_DETAILS_ENTITY, bargainDetails)
    }
    checkLogin(bundle, BargainPaymentActivity::class.java)
}

/**
 * 跳转-账号交易记录 com.kzd.btgame.main.home.BargainRecordActivity
 * */
fun toBargainRecordActivity() {
    checkLogin(BargainRecordActivity::class.java)
}

/**
 * 跳转-账号交易卖号 com.kzd.btgame.main.home.BargainSellActivity
 * */
fun toBargainSellActivity(bargainDetails: BargainDetails? = null) {
    val bundle = Bundle().apply {
        putSerializable(KEY_BARGAIN_DETAILS_ENTITY, bargainDetails)
    }
    checkLogin(bundle, BargainSellActivity::class.java)
}

/**
 * 跳转-领券中心 com.kzd.btgame.main.home.HomeCouponActivity
 * */
fun toHomeCouponActivity() {
    ActivityUtils.startActivity(HomeCouponActivity::class.java)
}

const val KEY_DOINGS_TITLE = "KEY_DOINGS_TITLE"
const val KEY_DOINGS_TYPE = "KEY_DOINGS_TYPE"
const val KEY_DOINGS_DOING = "KEY_DOINGS_DOING"

/**
 * 跳转-活动页面 com.kzd.btgame.main.home.HomeDoingsActivity
 *
 * @param title 标题
 * @param type 活动类型 'GMList' = '免费当GM', 'changwangList'='0元畅玩', 'chongzhiList' ='免费领充值', 'jingXuanList' ='精选IP大作',
 * */
fun toHomeDoingsActivity(title: String, type: String, doing: Doings<GameDoing>? = null) {
    val bundle = Bundle().apply {
        putString(KEY_DOINGS_TITLE, title)
        putString(KEY_DOINGS_TYPE, type)
        putSerializable(KEY_DOINGS_DOING, doing)
    }
    ActivityUtils.startActivity(bundle, HomeDoingsActivity::class.java)
}

/**
 * 跳转-游戏礼包 com.kzd.btgame.main.home.HomePresentActivity
 * */
fun toHomePresentActivity() {
    ActivityUtils.startActivity(HomePresentActivity::class.java)
}

/**
 * 跳转-搜索页面 com.kzd.btgame.main.home.HomeSearchActivity
 * */
fun toHomeSearchActivity() {
    ActivityUtils.startActivity(HomeSearchActivity::class.java)
}

/**
 * 跳转-开服表  com.kzd.btgame.main.home.HomeServiceActivity
 * */
fun toHomeServiceActivity() {
    ActivityUtils.startActivity(HomeServiceActivity::class.java)
}

const val KEY_HOME_LOTTERY = "KEY_HOME_LOTTERY"

/**
 * 跳转-抽奖页面 com.kzd.btgame.main.home.HomeLotteryActivity
 * */
fun toHomeLotteryActivity(lottery: GameLottery) {
    val bundle = Bundle().apply {
        putSerializable(KEY_HOME_LOTTERY, lottery)
    }
    ActivityUtils.startActivity(bundle, HomeLotteryActivity::class.java)
}

const val KEY_HOME_TRAVEL_INDEX = "KEY_HOME_TRAVEL_INDEX"

/**
 * 跳转-新游首发 com.kzd.btgame.main.game.GameTravelActivity
 * */
fun toGameTravelActivity(index: Int = 0) {
    val bundle = Bundle().apply {
        putString(KEY_HOME_TRAVEL_INDEX, index.toString())
    }
    ActivityUtils.startActivity(bundle, GameTravelActivity::class.java)
}


const val KEY_GAME_TITLE = "KEY_GAME_TITLE"
const val KEY_GAME_ICON = "KEY_GAME_ICON"
const val KEY_GAME_ID = "KEY_GAME_ID"

fun toGameH5Activity(title: String? = null, icon: String? = null, id: String? = null) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_TITLE, title)
        putString(KEY_GAME_ICON, icon)
        putString(KEY_GAME_ID, id)
    }
    checkLogin(bundle, GameH5Activity::class.java)
}

const val KEY_GAME_DETAILS_ID = "KEY_GAME_DETAILS_ID"
const val KEY_GAME_DETAILS_INDEX = "KEY_GAME_DETAILS_INDEX"

/**
 * 跳转-游戏详情页 com.kzd.btgame.main.game.GameDetailsActivity
 *
 * @param gid 游戏id
 * @param index 0=详情 1=福利 2=开服 3评论 4=交易
 * */
fun toGameDetailsActivity(gid: String, index: Int = 0) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_ID, gid)
        putString(KEY_GAME_DETAILS_INDEX, index.toString())
    }
    ActivityUtils.startActivity(bundle, GameDetailsActivity::class.java)
}

const val KEY_GAME_DETAILS_TITLE = "KEY_GAME_DETAILS_TITLE"
const val KEY_GAME_DETAILS_VIP = "KEY_GAME_DETAILS_VIP"
const val KEY_GAME_DETAILS_VIP_STR = "KEY_GAME_DETAILS_VIP_STR"

/**
 * 跳转-游戏详情VIP com.kzd.btgame.main.game.GameVipActivity
 *
 * @param title 游戏名称
 * @param gameVip 游戏vip列表
 * */
fun toGameVipActivity(title: String, gameVip: String, vipStr: String?) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_TITLE, title)
        putString(KEY_GAME_DETAILS_VIP, gameVip)
        putString(KEY_GAME_DETAILS_VIP_STR, vipStr)
    }
    ActivityUtils.startActivity(bundle, GameVipActivity::class.java)
}

/**
 * 跳转-发表评论 com.kzd.btgame.main.game.DiscussIssueActivity
 *
 * @param title 游戏名称
 * @param gid 游戏id
 * */
fun toDiscussIssueActivity(title: String, gid: String) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_TITLE, title)
        putString(KEY_GAME_DETAILS_ID, gid)
    }
    checkLogin(bundle, DiscussIssueActivity::class.java)
}

/**
 * 跳转-更多评论 com.kzd.btgame.main.game.DiscussMoreActivity
 *
 * @param title 游戏名称
 * @param gid 游戏id
 * */
fun toDiscussMoreActivity(title: String, gid: String) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_TITLE, title)
        putString(KEY_GAME_DETAILS_ID, gid)
    }
    ActivityUtils.startActivity(bundle, DiscussMoreActivity::class.java)
}

const val KEY_GAME_DETAILS_DISCUSS = "KEY_GAME_DETAILS_DISCUSS"

/**
 * 跳转-评论回复 com.kzd.btgame.main.game.DiscussReplyActivity
 *
 * @param title 游戏名称
 * @param gid 游戏id
 * @param gameDiscuss 评论条目
 * */
fun toDiscussReplyActivity(title: String, gid: String, gameDiscuss: GameDiscuss) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_TITLE, title)
        putString(KEY_GAME_DETAILS_ID, gid)
        putSerializable(KEY_GAME_DETAILS_DISCUSS, gameDiscuss)
    }
    ActivityUtils.startActivity(bundle, DiscussReplyActivity::class.java)
}

/**
 * 跳转-更多开服 com.kzd.btgame.main.game.ServiceMoreActivity
 *
 * @param title 游戏名称
 * @param gid 游戏id
 * */
fun toServiceMoreActivity(title: String, gid: String) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_TITLE, title)
        putString(KEY_GAME_DETAILS_ID, gid)
    }
    ActivityUtils.startActivity(bundle, ServiceMoreActivity::class.java)
}

/**
 * 跳转-收藏中心 com.kzd.btgame.main.mine.MineCollectActivity
 * */
fun toMineCollectActivity() {
    checkLogin(MineCollectActivity::class.java)
}

/**
 * 跳转-代金券 com.kzd.btgame.main.mine.MineCouponActivity
 * */
fun toMineCouponActivity() {
    checkLogin(MineCouponActivity::class.java)
}

/**
 * 跳转-下载管理 com.kzd.btgame.main.mine.MineDownloadActivity
 * */
fun toMineDownloadActivity() {
    ActivityUtils.startActivity(MineDownloadActivity::class.java)
}

/**
 * 跳转-意见反馈 com.kzd.btgame.main.mine.MineFeedbackActivity
 * */
fun toMineFeedbackActivity(title: String? = null, gid: String? = null) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_TITLE, title)
        putString(KEY_GAME_DETAILS_ID, gid)
    }
    checkLogin(bundle, MineFeedbackActivity::class.java)
}

/**
 * 跳转-我的足迹 com.kzd.btgame.main.mine.MineFootprintActivity
 * */
fun toMineFootprintActivity() {
    checkLogin(MineFootprintActivity::class.java)
}

/**
 * 跳转-我的礼包 com.kzd.btgame.main.mine.MineGiftActivity
 * */
fun toMineGiftActivity() {
    checkLogin(MineGiftActivity::class.java)
}

/**
 * 跳转-立即邀请 com.kzd.btgame.main.mine.MineInviteActivity
 * */
fun toMineInviteActivity() {
    checkLogin(MineInviteActivity::class.java)
}

const val KEY_MEMBER_INDEX = "KEY_MEMBER_INDEX"

/**
 * 跳转-省钱卡会员中心 com.kzd.btgame.main.mine.MineMemberActivity
 *
 * @param index 下标
 * */
fun toMineMemberActivity(index: Int = 0) {
    val bundle = Bundle().apply {
        putString(KEY_MEMBER_INDEX, index.toString())
    }
    checkLogin(bundle, MineMemberActivity::class.java)
}

/**
 * 跳转-消息中心 com.kzd.btgame.main.mine.MineMessageActivity
 * */
fun toMineMessageActivity() {
    ActivityUtils.startActivity(MineMessageActivity::class.java)
}

const val KEY_MESSAGE_ENTITY = "KEY_MESSAGE_ENTITY"

/**
 * 跳转-消息详情 com.kzd.btgame.main.mine.MessageDetailsActivity
 *
 * @param userMessage 消息条目
 * */
fun toMessageDetailsActivity(userMessage: UserMessage) {
    val bundle = Bundle().apply {
        putSerializable(KEY_MESSAGE_ENTITY, userMessage)
    }
    ActivityUtils.startActivity(bundle, MessageDetailsActivity::class.java)
}

/**
 * 跳转-平台币购买 com.kzd.btgame.main.mine.MinePlatformActivity
 * */
fun toMinePlatformActivity() {
    checkLogin(MinePlatformActivity::class.java)
}

/**
 * 跳转-申请返利 com.kzd.btgame.main.mine.MineRebateActivity
 * */
fun toMineRebateActivity() {
    checkLogin(MineRebateActivity::class.java)
}

const val KEY_RECORD_BILL_INDEX = "KEY_RECORD_BILL_INDEX"

/**
 * 跳转-我的账单 com.kzd.btgame.main.mine.MineRecordBillActivity
 *
 * @param index 下标
 * */
fun toMineRecordBillActivity(index: Int = 0) {
    val bundle = Bundle().apply {
        putString(KEY_RECORD_BILL_INDEX, index.toString())
    }
    checkLogin(bundle, MineRecordBillActivity::class.java)
}

/**
 * 跳转-中奖记录 com.kzd.btgame.main.mine.MineRecordLotteryActivity
 * */
fun toMineRecordLotteryActivity() {
    checkLogin(MineRecordLotteryActivity::class.java)
}

/**
 * 跳转-设置中心 com.kzd.btgame.main.mine.MineSettingActivity
 * */
fun toMineSettingActivity() {
    ActivityUtils.startActivity(MineSettingActivity::class.java)
}

/**
 * 跳转-充值转游 com.kzd.btgame.main.mine.MineTransferActivity
 * */
fun toMineTransferActivity() {
    checkLogin(MineTransferActivity::class.java)
}

/**
 * 跳转-转游福利 com.kzd.btgame.main.mine.TransferDetailsActivity
 * */
fun toTransferDetailsActivity(gid: String) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_ID, gid)
    }
    ActivityUtils.startActivity(bundle, TransferDetailsActivity::class.java)
}

/**
 * 跳转-转游申请 com.kzd.btgame.main.mine.TransferApplyActivity
 * */
fun toTransferApplyActivity(gid: String) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_ID, gid)
    }
    ActivityUtils.startActivity(bundle, TransferApplyActivity::class.java)
}

/**
 * 跳转-个人中心 com.kzd.btgame.main.mine.MineUserActivity
 * */
fun toMineUserActivity() {
    checkLogin(MineUserActivity::class.java)
}

const val KEY_ADDRESS_INFO = "KEY_ADDRESS_INFO"

/**
 * 跳转-地址管理 com.kzd.btgame.main.mine.MineAddressActivity
 * */
fun toMineAddressActivity(activity: Activity, requestCode: Int) {
    ActivityUtils.startActivityForResult(activity, MineAddressActivity::class.java, requestCode)
}

/**
 * 跳转-我的体验金 com.kzd.btgame.main.mine.MoneyExperienceActivity
 * */
fun toMoneyExperienceActivity() {
    ActivityUtils.startActivity(MoneyExperienceActivity::class.java)
}

/**
 * 跳转-积分商城 com.kzd.btgame.main.money.MoneyMallListActivity
 * */
fun toMoneyMallListActivity() {
    ActivityUtils.startActivity(MoneyMallListActivity::class.java)
}

const val KEY_MONEY_MALL_ID = "KEY_MONEY_MALL_ID"

/**
 * 跳转-积分商城详情 com.kzd.btgame.main.money.MoneyMallDetActivity
 *
 * @param id 积分商城id
 * */
fun toMoneyMallDetActivity(id: String) {
    val bundle = Bundle().apply {
        putString(KEY_MONEY_MALL_ID, id)
    }
    ActivityUtils.startActivity(bundle, MoneyMallDetActivity::class.java)
}

const val KEY_MONEY_MALL_DET = "KEY_MONEY_MALL_DET"

/**
 * 跳转-积分商城兑换 com.kzd.btgame.main.money.MoneyMallSubActivity
 *
 * @param mallDetails 积分商城详情
 * */
fun toMoneyMallSubActivity(mallDetails: MallDetails) {
    val bundle = Bundle().apply {
        putSerializable(KEY_MONEY_MALL_DET, mallDetails)
    }
    ActivityUtils.startActivity(bundle, MoneyMallSubActivity::class.java)
}


const val KEY_MONEY_TASK_TYPE = "KEY_MONEY_TASK_TYPE"

/**
 * 跳转-任务详情 com.kzd.btgame.main.money.MoneyTaskActivity
 *
 * @param type 任务类型
 * */
fun toMoneyTaskActivity(type: String) {
    val bundle = Bundle().apply {
        putString(KEY_MONEY_TASK_TYPE, type)
    }
    ActivityUtils.startActivity(bundle, MoneyTaskActivity::class.java)
}

/**
 * 跳转-大佬须知 com.kzd.btgame.main.money.MoneyTaskBossActivity
 * */
fun toMoneyTaskBossActivity() {
    ActivityUtils.startActivity(MoneyTaskBossActivity::class.java)
}

/**
 * 跳转-试玩任务 com.kzd.btgame.main.money.MoneyTaskPlayActivity
 * */
fun toMoneyTaskPlayActivity() {
    ActivityUtils.startActivity(MoneyTaskPlayActivity::class.java)
}

/**
 * 跳转-问题详情 com.kzd.btgame.main.service.ProblemDetailsActivity
 * */
const val KEY_PROBLEM_ACTION = "KEY_PROBLEM_ACTION"
fun toProblemDetailsActivity(action: ProblemDetailsAction) {
    val bundle = Bundle().apply {
        putSerializable(KEY_PROBLEM_ACTION, action)
    }
    ActivityUtils.startActivity(bundle, ProblemDetailsActivity::class.java)
}

const val KEY_RICH_TEXT_TITLE = "KEY_RICH_TEXT_TITLE"
const val KEY_RICH_TEXT_URL = "KEY_RICH_TEXT_URL"
const val KEY_RICH_TEXT_CONTENT = "KEY_RICH_TEXT_CONTENT"
const val KEY_RICH_TEXT_TYPE = "KEY_RICH_TEXT_TYPE"

fun toMineRichTextActivityUrl(
    title: String? = null, url: String?, isLogin: Boolean = false,
    action: AccessAction = AccessAction.LOGIN,
) {
    val bundle = Bundle().apply {
        putString(KEY_RICH_TEXT_TITLE, title)
        putString(KEY_RICH_TEXT_URL, url)
    }
    if (isLogin) {
        checkLogin(bundle, MineRichTextActivity::class.java, action)
    } else {
        ActivityUtils.startActivity(bundle, MineRichTextActivity::class.java)
    }
}

fun toMineRichTextActivityCon(title: String? = null, content: String?) {
    val bundle = Bundle().apply {
        putString(KEY_RICH_TEXT_TITLE, title)
        putString(KEY_RICH_TEXT_CONTENT, content)
    }
    ActivityUtils.startActivity(bundle, MineRichTextActivity::class.java)
}

fun toMineRichTextActivityApi(title: String? = null, type: String?) {
    val bundle = Bundle().apply {
        putString(KEY_RICH_TEXT_TITLE, title)
        putString(KEY_RICH_TEXT_TYPE, type)
    }
    ActivityUtils.startActivity(bundle, MineRichTextActivity::class.java)
}

/**
 * 跳转-外部应用QQ
 * */
fun toExternalAppQQ(activity: Activity) {
    //val url = "mqqwpa://im/chat?chat_type=wpa&uin=2850227014"
    val url = "https://wpa1.qq.com/CnFgdQWE?_type=wpa&qidian=true"
    activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
}